package com.apollow.oschina.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.apollow.oschina.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 创建自定义的dialog，个人资料头像点击弹出
 * Created by chengguo on 2016/3/22.
 */
public class SelfDialog extends Dialog {

    @BindView(R.id.title)
    TextView mTitle;
    @BindView(R.id.tv_change)
    TextView mTvChange;
    @BindView(R.id.tv_look_big)
    TextView mTvLookBig;
    @BindView(R.id.no)
    Button mNo;
    private String mTitleStr;//从外界设置的title文本
    private String mChangeStr;//从外界设置的消息文本
    private String mLookBigStr;//从外界设置的消息文本
    //取消文本的显示内容
    private String noStr;

    private onNoOnclickListener noOnclickListener;//取消按钮被点击了的监听器  
    private onChangeOnclickListener onChangeOnclickListener;
    private onLookBigOnclickListener onLookBigOnclickListener;
    /**
     * 设置取消按钮的显示内容和监听
     *
     * @param str
     * @param onNoOnclickListener
     */
    public void setNoOnclickListener(String str, onNoOnclickListener onNoOnclickListener) {
        if (str != null) {
            noStr = str;
        }
        this.noOnclickListener = onNoOnclickListener;
    }

    public void setChangeOnclickListener(onChangeOnclickListener onChangeOnclickListener){
        this.onChangeOnclickListener = onChangeOnclickListener;
    }

    public void setLookBigOnclickListener(onLookBigOnclickListener onLookBigOnclickListener){
        this.onLookBigOnclickListener = onLookBigOnclickListener;
    }

    public SelfDialog(Context context) {
        super(context, R.style.MyDialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_my_dialog);
        ButterKnife.bind(this);
        //按空白处不能取消动画  
        setCanceledOnTouchOutside(true);
        //初始化界面数据
        initData();
        //初始化界面控件的事件  
        initEvent();

    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {
        //设置取消按钮被点击后，向外界提供监听
        mNo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (noOnclickListener != null) {
                    noOnclickListener.onNoClick();
                }
            }
        });

        mTvChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onChangeOnclickListener != null){
                    onChangeOnclickListener.onChangeClick();
                }
            }
        });

        mTvLookBig.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onLookBigOnclickListener != null){
                    onLookBigOnclickListener.onLookBigClick();
                }
            }
        });
    }

    /**
     * 初始化界面控件的显示数据
     */
    private void initData() {
        //如果用户自定了title和message  
        if (mTitleStr != null) {
            mTitle.setText(mTitleStr);
        }
        if (mChangeStr != null) {
            mTvChange.setText(mChangeStr);
        }
        if (mLookBigStr != null) {
            mTvLookBig.setText(mLookBigStr);
        }
        //如果设置按钮的文字  
        if (noStr != null) {
            mNo.setText(noStr);
        }
    }

    /**
     * 从外界Activity为Dialog设置标题
     *
     * @param title
     */
    public void setTitle(String title) {
        mTitleStr = title;
    }

    /**
     * 从外界Activity为Dialog设置dialog的message
     *
     * @param changeStr
     */
    public void setMessage(String changeStr,String lookBigStr) {
        this.mChangeStr = changeStr;
        this.mLookBigStr = lookBigStr;
    }

    public interface onNoOnclickListener {
        public void onNoClick();
    }

    public interface onChangeOnclickListener {
        public void onChangeClick();
    }

    public interface onLookBigOnclickListener{
        public void onLookBigClick();
    }
} 